﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.fmtx.Model;
using com.fmtx.IDal;

namespace com.fmtx.Dal
{
    public class CityServer:BaseServer<City>,ICity
    {
        public bool IsExists(string name)
        {
            var model = db.AllList(w => w.Name == name).FirstOrDefault();
            return model!=null&& model.Id > 0;
        }

        public List<City> QueryByName(string name, int num)
        {
            var list = db.AllList(w => w.Name.Contains(name) || w.EnName.Contains(name)||w.PingYin.Contains(name));
            return list.Take(num).ToList();
        }


        public IList<City> Query(int index, int size, string name, int countryId,out int count)
        {
            
            var list = db.AllList();
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(w => w.Name.Contains(name));
            }
            if (countryId > 0)
            {
                list = list.Where(w => w.CountryId == countryId);
            }
            count = list.Count();
            return list.OrderBy(o => o.Id).Skip(index * size).Take(size).ToList();
        }


        public IList<City> QueryIdAndName()
        {
            var query = from q in db.AllList()
                        select new City
                        {
                            Id = q.Id,
                            Name = q.Name
                        };
            return query.ToList();
        }


        public IList<City> QueryByCountry(int cid,int index,int size,ref int count)
        {
            var list= db.AllList(w => w.CountryId == cid);
            count = list.Count();
            return list.OrderByDescending(o => o.Id).Skip(index * size).Take(size).ToList();
        }
    }
}
